package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.ydd.common.R;
import com.itheima.ydd.dto.PageInfo;
import com.itheima.ydd.pojo.YddCate;
import com.itheima.ydd.service.YddCateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("ydd_cate")
public class YddCateController {
    @Autowired
    private YddCateService yddCateService;

    /**
     * 查询单个,用于修改回显
     * @param id
     * @return
     */
    @GetMapping("rowInfo")
    public ResponseEntity<R<YddCate>> rowInfo(@RequestParam String id){
        YddCate yddCate = yddCateService.getById(id);
        log.info(yddCate.toString());
        return ResponseEntity.ok(R.success(yddCate));
    }

    /**
     * 带条件的分页查询
     * @param catename
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("list")
    public ResponseEntity<R<PageInfo<YddCate>>> findPageOrName(@RequestParam("catename") String catename,
                                                            @RequestParam("page") String page,
                                                            @RequestParam("limit") String limit){
        return yddCateService.findPageOrName(catename,page,limit);
}

    /**
     * 根据分类id进行删除
     * 删除分类管理时,需要判断ydd_article下面cateid没有数据
     * @param id
     * @return
     */
    @GetMapping("del")
    public ResponseEntity<R> removeWishYddArticle(@RequestParam String id){
        return yddCateService.removeWishYddArticle(id);
    }

    /**
     * 添加分类
     * @param yddCate
     * @return
     */
    @PostMapping("add")
    public ResponseEntity<R> save(@RequestBody YddCate yddCate){
        return yddCateService.saveYddCate(yddCate);
    }

    /**
     * 修改功能
     * @param yddCate
     * @return
     */
    @PostMapping("update")
    public ResponseEntity<R> updateYddCate(@RequestBody YddCate yddCate){
        return yddCateService.updateYddCate(yddCate);
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping("all")
    public ResponseEntity<R<List<YddCate>>> findAll(){
        return yddCateService.findAll();
    }


}
